package com.genius.unite_platform.busswork.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.menu.entity.MenuJson;
import com.genius.adminmanager.menu.service.MenuUtils;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.adminmanager.other.entity.SessionJson;
import com.genius.adminmanager.role.entity.RoleJson;
import com.genius.unite_platform.busswork.entity.BussWorkEntity;
import com.genius.unite_platform.busswork.service.BussWorkServiceAdapter;
import com.genius.workflow.page.PageResult;
import com.genius.workflow.page.SearchResult;
import com.genius.workflow.page.SqlLang;
import com.genius.workflow.util.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;

@Service("BussWorkService")
public class BussWorkSelectServiceImpl extends BussWorkServiceAdapter{

	@Override
	public JSONObject getSelectBody(JSONObject json, JSONObject response) {
		int limit = json.getIntValue(PageResult.limit);
        int page = json.getIntValue(PageResult.page);    
        SqlLang sql = null;
        String  jobnum  = json.getString("jobnumber");
        if  (jobnum.equals("0"))
        {
        	jobnum="";
        }
        System.out.println(jobnum);
        String selectSB = " select sStoreNO as shopcode,sStoreName as shopname\r\n"
        		+ " from jhcvs.dict_tuserstorehistory \r\n"
        		+ "where dTradeDate=(select max(dTradeDate) from  jhcvs.dict_tuserstorehistory ) \r\n"
        		+ "and   sEmployeeNO like '"+jobnum+"%'\r\n";        	 		
        System.out.println(selectSB);
        String headField = json.getString(PageResult.headField);
        if (json.containsKey(PageResult.asc)) {        
            String ascField = json.getString(PageResult.ascField);
            String asc = json.getString(PageResult.asc);            
            sql = DBTools2.getSelectPageSql(headField, selectSB, page, limit, ascField, asc, null);
        }else{        	 
            String asc = null;
            String ascField = null;            
            sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc,null);
          
        }
        try {//   
            PageResult result = DBTools2.selectByCount(sql, null);
            JSONArray data = (JSONArray) result.getData();
            response.put(ResponseJson.count, result.getCount());
            response.put(ResponseJson.data, data);
            response.put(ResponseJson.status, ResponseJson.status_Success);
        } catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
        return response;
    }
	

	@Override
	public JSONObject getGoodsInfo(JSONObject json, JSONObject response) {
		int limit = json.getIntValue(PageResult.limit);
        int page = json.getIntValue(PageResult.page);    
        System.out.println(json);
        SqlLang sql = null;
        String selectSB = "select *  from  jhcvs.goods_info where gdsincode='"+json.getString("goodsno")+"'";  
        String headField = json.getString(PageResult.headField);
        if (json.containsKey(PageResult.asc)) {        
            String ascField = json.getString(PageResult.ascField);
            String asc = json.getString(PageResult.asc);            
            sql = DBTools2.getSelectPageSql(headField, selectSB, page, limit, ascField, asc, null);
        }else{        	 
            String asc = null;
            String ascField = null;            
            sql = DBTools2.getSelectPageSql(headField,selectSB,page,limit,ascField,asc,null);
          
        }
        try {//   
            PageResult result = DBTools2.selectByCount(sql, null);
            JSONArray data = (JSONArray) result.getData();
            response.put(ResponseJson.count, result.getCount());
            response.put(ResponseJson.data, data);
            response.put(ResponseJson.status, ResponseJson.status_Success);
        } catch (ConnectionException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_No_Porwer_Query_Field);
        } catch (DBClosedException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_DB_Closed_Failed);
        } catch (SQLException e) {
            response.put(ResponseJson.status, ResponseJson.status_Failed);
            response.put(ResponseJson.msg, ResponseJson.msg_Query_Data_Error);
        }
        return response;
    }
}

